{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 客户违约预测模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.5.1 案例背景"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "银行等金融机构经常会根据客户的个人资料、财产等情况，来预测借款客户是否会违约，从而进行贷前审核，贷中管理，贷后违约处理等工作。金融处理的就是风险，需要在风险和收益间寻求到一个平衡点，现代金融某种程度上便是一个风险定价的过程，通过个人的海量数据，从而对其进行风险评估并进行合适的借款利率定价，这便是一个典型的风险定价过程，这也被称之为大数据风控。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.模型搭建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收入</th>\n",
       "      <th>年龄</th>\n",
       "      <th>性别</th>\n",
       "      <th>历史授信额度</th>\n",
       "      <th>历史违约次数</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>462087</td>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>362324</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>13583</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>332011</td>\n",
       "      <td>52</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>252895</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>352355</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       收入  年龄  性别  历史授信额度  历史违约次数  是否违约\n",
       "0  462087  26   1       0       1     1\n",
       "1  362324  32   0   13583       0     1\n",
       "2  332011  52   1       0       1     1\n",
       "3  252895  39   0       0       1     1\n",
       "4  352355  50   1       0       0     1"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.读取数据\n",
    "import pandas as pd\n",
    "df = pd.read_excel('客户信息及违约表现.xlsx')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,\n",
       "               importance_type='split', learning_rate=0.1, max_depth=-1,\n",
       "               min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,\n",
       "               n_estimators=100, n_jobs=-1, num_leaves=31, objective=None,\n",
       "               random_state=None, reg_alpha=0.0, reg_lambda=0.0, silent=True,\n",
       "               subsample=1.0, subsample_for_bin=200000, subsample_freq=0)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.提取特征变量和目标变量\n",
    "X = df.drop(columns='是否违约')\n",
    "Y = df['是否违约']\n",
    "\n",
    "# 3.划分训练集和测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=123)\n",
    "\n",
    "# 4.模型训练及搭建\n",
    "from lightgbm import LGBMClassifier\n",
    "model = LGBMClassifier()\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过如下代码可以查看官方讲解\n",
    "# LGBMClassifier?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.模型预测及评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 1 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 1 0\n",
      " 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1\n",
      " 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 1 0 1 1 0\n",
      " 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1\n",
      " 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0\n",
      " 0 0 1 1 0 1 0 0 1 1 0 1 0 1 1]\n"
     ]
    }
   ],
   "source": [
    "# 预测测试集数据\n",
    "y_pred = model.predict(X_test)\n",
    "print(y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>预测值</th>\n",
       "      <th>实际值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   预测值  实际值\n",
       "0    1    1\n",
       "1    0    1\n",
       "2    1    1\n",
       "3    0    0\n",
       "4    0    1"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测值和实际值对比\n",
    "a = pd.DataFrame()  # 创建一个空DataFrame \n",
    "a['预测值'] = list(y_pred)\n",
    "a['实际值'] = list(y_test)\n",
    "a.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.795"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.795"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看得分\n",
    "model.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAPs0lEQVR4nO3db4hld33H8ffHpKm0TbR0V1j3j5vSDTiGojLEiFAjxrIJuPvE2g2ItQRXbWMfKIUUS5T4qEorCIu6tGIVYhJ9YAZZSamNWMRNdyQxmg0p26jJJEsz2jR5IBpDv31wZ/U6e2fumZk798793fcLFu455zf3fn+5s59893fPuSdVhSRp+r1o0gVIkkbDQJekRhjoktQIA12SGmGgS1IjLp3UC+/atasOHjw4qZeXpKn0ne9858dVtXvQsYkF+sGDB1lcXJzUy0vSVEryo7WOueQiSY0w0CWpEQa6JDXCQJekRhjoktSIoYGe5LNJnk7y/TWOJ8knk5xL8lCS146+TEnSMF069M8Bh9c5fgNwaOXPceBTWy9LkrRRQ89Dr6pvJjm4zpCjwOer9z28p5O8NMmeqjo/oholaWrccf/j3PPgk+uOmXv5FXz4ra8a+WuPYg19L/BE3/bSyr6LJDmeZDHJ4vLy8gheWpJ2lnsefJKz55+byGuP4krRDNg38K4ZVXUSOAkwPz/vnTUkNWluzxXc9Z7Xj/11RxHoS8D+vu19wFMjeF5JGqkuyyFbdfb8c8ztuWJbX2Mto1hyWQDeuXK2y7XAs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cDVwDeS/JDeWuPClH8w2vV3+56q+kVV/QB4lF7AT6suc74ZuBugqr4NvJjel1i1qtPf943YyYE+izenHjrnleWHz9AL82lfV4Uhc66qZ6tqV1UdrKqD9D43OFJVi5MpdyS6/G5/hd4H4CTZRW8J5rGxVjlaXeb8OPBmgCSvpBfoy2OtcrwWgHeunO1yLfBsVZ3f0jNO+pPgIZ8S3wj8J71Pxz+0su92en+hofeGfwk4B/wH8PuTrnkMc/5X4L+BB1f+LEy65u2e86qx32DKz3Lp+D4H+AfgLPA94Nikax7DnOeAb9E7A+ZB4I8nXfMW5/tF4DzwC3rd+M3Ae4H39r3HJ1b+e3xvFL/XXvovSY3YyUsukqQNMNAlqREGuiQ1wkCXpEYY6JLUCANdkhphoEtSI/4fwAJCf085lIwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查看预测属于各个分类的概率\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "\n",
    "# 绘制ROC曲线\n",
    "from sklearn.metrics import roc_curve\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(fpr, tpr)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.82250025429763"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# AUC值\n",
    "from sklearn.metrics import roc_auc_score\n",
    "score = roc_auc_score(y_test, y_pred_proba[:,1])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1127,  655,  134,  874,  154])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征重要性\n",
    "model.feature_importances_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>特征名称</th>\n",
       "      <th>特征重要性</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>收入</td>\n",
       "      <td>1127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>历史授信额度</td>\n",
       "      <td>874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>年龄</td>\n",
       "      <td>655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>历史违约次数</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>性别</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     特征名称  特征重要性\n",
       "0      收入   1127\n",
       "3  历史授信额度    874\n",
       "1      年龄    655\n",
       "4  历史违约次数    154\n",
       "2      性别    134"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features = X.columns  # 获取特征名称\n",
    "importances = model.feature_importances_  # 获取特征重要性\n",
    "\n",
    "# 通过二维表格形式显示\n",
    "importances_df = pd.DataFrame()\n",
    "importances_df['特征名称'] = features\n",
    "importances_df['特征重要性'] = importances\n",
    "importances_df.sort_values('特征重要性', ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.模型参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 参数调优\n",
    "from sklearn.model_selection import GridSearchCV  # 网格搜索合适的超参数\n",
    "parameters = {'num_leaves': [10, 15, 31], 'n_estimators': [10, 20, 30], 'learning_rate': [0.05, 0.1, 0.2]}\n",
    "model = LGBMClassifier()  # 构建分类器\n",
    "grid_search = GridSearchCV(model, parameters, scoring='roc_auc', cv=5)  # cv=5表示交叉验证5次，scoring='roc_auc'表示以ROC曲线的AUC评分作为模型评价准则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'learning_rate': 0.1, 'n_estimators': 20, 'num_leaves': 15}"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输出参数最优值\n",
    "grid_search.fit(X_train, y_train)  # 传入数据\n",
    "grid_search.best_params_  # 输出参数的最优值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,\n",
       "               importance_type='split', learning_rate=0.1, max_depth=-1,\n",
       "               min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,\n",
       "               n_estimators=20, n_jobs=-1, num_leaves=15, objective=None,\n",
       "               random_state=None, reg_alpha=0.0, reg_lambda=0.0, silent=True,\n",
       "               subsample=1.0, subsample_for_bin=200000, subsample_freq=0)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重新搭建分类器\n",
    "model = LGBMClassifier(num_leaves=15, n_estimators=20,learning_rate=0.1)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAWIklEQVR4nO3de3CV9Z3H8fc3hIDhLgkXuQUUBIRa3IiXWkWxFt0d6MW64La1XUa0Le5O7XRrpx23azvV6rRunbJtaXVUWuttXU27WLv1Uq0rlqBUAUGyyCUETUBuAgm5fPePc2hjSHKekOecJ+eXz2smM+c5z+88z/fHCR9+/J6buTsiIpL/CpIuQERE4qFAFxEJhAJdRCQQCnQRkUAo0EVEAlGY1I5LSkq8rKwsqd2LiOSlNWvW7Hb30vbWJRboZWVlVFZWJrV7EZG8ZGbbOlqnKRcRkUAo0EVEAqFAFxEJhAJdRCQQCnQRkUBkDHQzu8fMas1sXQfrzczuMrMqM3vNzM6Kv0wREckkygj9XmBeJ+svByanf5YAP+5+WSIi0lUZz0N39+fNrKyTJguA+z11H95VZjbUzEa7+66YahQRSYy789grO9m251Bs25w7bSRnjhsa2/aOiePCojHAjlbL1en3jgt0M1tCahTP+PHjY9i1iEj21Dc2c9N/vsbja2sAMItnuyMG9++xgd5eF9t9aoa7LweWA5SXl+vJGiISi41vH+BgfVOs22xuce54ahNrtu3lqx89nS/OORWLK9GzJI5ArwbGtVoeC9TEsF0RkU41Nbfw7d9s4L6XOrwavlv6FRbwH/9wFlfMHJ2V7cctjkCvAJaa2YPAOcB+zZ+LSLYdqG9k6QOv8vybdXz+Q2XMnToy9n2UlRQzdlhx7NvNloyBbma/AuYAJWZWDfwr0BfA3X8CrASuAKqAw8Dns1WsiAjA9j2HWXzfat7afYhbPzGTRbN1TA6ineWyKMN6B74UW0Ui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NppAK9LqdV5lYF8Nn02S7nAvvdfVe3tpj0keAMR4mvAN4kdXT8G+n3biH1FxpSX/gjQBXwJ2BS0jXnoM+/B94B1qZ/KpKuOdt9btP2OfL8LJeI37MBPwA2AK8DC5OuOQd9ng68SOoMmLXAZUnX3M3+/grYBTSSGo0vBq4Hrm/1HS9L/3m8HsfvtS79FxEJRE+echERkS5QoIuIBEKBLiISCAW6iEggFOgiIoFQoIuIBEKBLiISiP8HNzKYwwG5LysAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查看ROC曲线\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "from sklearn.metrics import roc_curve\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(fpr, tpr)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8615603702573491"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看AUC值\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "from sklearn.metrics import roc_auc_score\n",
    "score = roc_auc_score(y_test, y_pred_proba[:, 1])\n",
    "score"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
